// pages/lang/lang.js

import { saveUserLanguage } from '../../api/user/user'
import { _t } from '../../i18n/base'
Page({
  data: {
    index: '1',
    userId: '',
  },

  // 中文
  async norChi() {
    this.setData({
      index: '1',
    })
    let { result } = await saveUserLanguage({
      language: 1,
      userId: this.data.userId,
    })
    console.log(result);
    wx.setStorageSync('lang', result.language)

    wx.setStorageSync('language', 'zh')
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  // 繁体
  async fanChi() {
    this.setData({
      index: '2',
    })
    let { result } = await saveUserLanguage({
      language: 2,
      userId: this.data.userId,
    })
    console.log(result);
    wx.setStorageSync('lang', result.language)

    wx.setStorageSync('language', 'tw')
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  // 英文
  async english() {
    this.setData({
      index: '3',
    })
    let { result } = await saveUserLanguage({
      language: 3,
      userId: this.data.userId,
    })
    wx.setStorageSync('lang', result.language)
    wx.setStorageSync('language', 'en')
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  onLoad() {
    let userId = wx.getStorageSync('userId')
    this.setData({
      userId,
      lang: _t(),
    })
    let data = {}
    let lang = wx.getStorageSync('language') || 'zh'
    if (lang == 'en') {
      data.index = '3'
    } else if (lang == 'zh') {
      data.index = '1'
    } else {
      data.index = '2'
    }
    wx.setNavigationBarTitle({
      title: this.data.lang['语言偏好'],
    })
    this.setData(data)
  },
})
